package com.yuan.mint.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.yuan.mint.R;
import com.yuan.mint.utils.AppManager;
import com.yuan.mint.utils.DataCleanManager;
import com.yuan.mint.widget.RoundImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener {

    private static final int WEIGHT_REPORT = 0;
    private static final int MY_FAVORITE = 1;
    private static final int CLEAR_CACH = 2;
    private static final int EXIT = 3;

    private SimpleAdapter myAdapter;
    private List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    private RelativeLayout userLayout;
    private RoundImageView userImg;
    private TextView userName;
    private TextView editUser;
    private ListView settingList;

    private String str[] = new String[]{"体重报告", "我的收藏", "清除缓存", "退出"};
    private int img[] = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher, R.mipmap.ic_launcher};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        userLayout = (RelativeLayout) view.findViewById(R.id.user_info);
        userLayout.setOnClickListener(this);
        userImg = (RoundImageView) view.findViewById(R.id.user_img);
        userName = (TextView) view.findViewById(R.id.username);
        editUser = (TextView) view.findViewById(R.id.edit_user);
        settingList = (ListView) view.findViewById(R.id.setting_info_list);
        initView(view);
        return view;
    }

    private void initView(View view) {
        getItemData();
        myAdapter = new SimpleAdapter(view.getContext(), listData,
                R.layout.recommend_list_item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});
        settingList.setAdapter(myAdapter);
        settingList.setOnItemClickListener(this);
    }

    //用来实例化列表容器的函数
    private void getItemData() {
        for (int i = 0; i < str.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("img", img[i]);
            map.put("text", str[i]);
            listData.add(map);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case WEIGHT_REPORT:
                break;
            case MY_FAVORITE:
                break;
            case CLEAR_CACH:
                DataCleanManager.cleanApplicationData(getActivity());
                Toast.makeText(getActivity(), "已清除", Toast.LENGTH_SHORT).show();
                break;
            case EXIT:
                AppManager.getInstance().AppExit(getActivity());
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }
}
